const {
  queryWorkOrderById
} = require('../../api/workOrder')
const {
  USER_TYPE,
  WORK_ORDER_STATUS
} = require('../../constant/index');
const {
  getUserType,
  getUserBaseInformation
} = require('../../utils/strorage')
const {
  to,
  openLoading,
  closeLoading,
} = getApp()
Page({
  data: {
    USER_TYPE,
    WORK_ORDER_STATUS,
    wordCardData: null,
    tabsList: [],
    userType: '',
    isLaunchUserSelf: true,
    isMaintainerSelf: true
  },

  onLoad() {
    this.getUserKind();
    this.getUserDataById()
  },

  getUserKind() {
    const userType = getUserType();
    const userInfo = getUserBaseInformation()
    let approvalSupplementaryWorkOrder = 0
    if (userInfo) {
      approvalSupplementaryWorkOrder = userInfo.approvalSupplementaryWorkOrder
    }

    this.setData({
      userType,
      approvalSupplementaryWorkOrder
    })
  },

  async getUserDataById() {
    const params = {
      workOrderId: this.options.workOrderId
    };
    openLoading()
    const [err, data] = await to(queryWorkOrderById(params));
    closeLoading()
    if (!err) {
      const launchUserId = data.launchUserId
      const maintainerId = data.maintainerId
      const userId = getUserBaseInformation().userId

      let isLaunchUserSelf = true;

      if (launchUserId !== userId) {
        isLaunchUserSelf = false
      }

      let isMaintainerSelf = true;

      if (maintainerId !== userId) {
        isMaintainerSelf = false
      }
      this.setData({
        wordCardData: data,
        isLaunchUserSelf,
        isMaintainerSelf
      })

      const tabsList = [{
          value: 0,
          name: '工单详情'
        },
        {
          value: 1,
          name: '工单流程'
        },
      ]

      if (data.partsInOutVOS && data.partsInOutVOS.length) {
        tabsList.push({
          value: 2,
          name: '备件'
        })
      }
      if (data.starClass) {
        tabsList.push({
          value: 3,
          name: '评价'
        })

      }

      this.setData({
        tabsList
      })
    }
  },

  onClickApprove(status) {
    const component = this.selectComponent('#orderForm');
    if (component.onClickApprove) {
      component.onClickApprove(status)
    }
  }
})